package scala.test

import scala.collection.immutable
import scala.io.Source

/**
  * 1、统计总分年级排名前十学生各科的分数
  */
object Test03 {
  def main(args:Array[String]):Unit ={
    //读数据
    val scores: List[String] = Source.fromFile("data/score.txt").getLines().toList
    //取数据
    val stuData: List[(String, Int,String)] = scores.map((data:String)=>{
      val split: Array[String] = data.split(",")
      val id: String = split(0)
      val subjectId: String = split(1)
      val score: Int = split(2).toInt
      (id,score,subjectId)
    })
    //根据学生id分组
    val group: Map[String, List[(String, Int,String)]] = stuData.groupBy((kv:(String,Int,String))=>kv._1)
    //求学生的总分
    val idAndScoreSum: immutable.Iterable[(String, Int, List[Int])] = group.map((kv:(String,List[(String,Int,String)]))=>{
      val id: String = kv._1
      val scores: List[(String, Int,String)] = kv._2
      val scoreNum: List[Int] =  scores.map((f:(String,Int,String)) =>{
        val score: Int = f._2
        score
      })
      val scoreSum: Int = scoreNum.sum
      (id,scoreSum,scoreNum)
    })
    val stuList = idAndScoreSum.toList.sortBy((f:(String,Int,List[Int]))=> -(f._2)).take(10)
    stuList.foreach(println)
  }
}
